﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace SaturniLibrary.Utility
{
    public enum WatermarkPosition
    {
        TopLeft,
        TopCenter,
        TopRight,
        MiddleLeft,
        MiddleCenter,
        MiddleRight,
        BottomLeft,
        BottomCenter,
        BottomRight
    }

    public class ImageWatermark
    {
        public static void DrawImage(string imageInputPath, string imageOutputPath, string watermarkImagePath, int x, int y, int width, int height)
        {
            DrawImage(imageInputPath, imageOutputPath, watermarkImagePath, new Rectangle(x, y, width, height));
        }

        public static void DrawImage(string imageInputPath, string imageOutputPath, string watermarkImagePath, Rectangle rectangle)
        {
            Image image = Image.FromFile(imageInputPath);
            Image copyImage = Image.FromFile(watermarkImagePath);
            Graphics g = Graphics.FromImage(image);
            g.DrawImage(copyImage, rectangle, 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
            //g.DrawImage(水印图象名,水印区域大小(x坐标,y坐标,宽,高),水印图象x坐标,水印图象y坐标,水印图象宽,水印图象高,方式)
            g.Dispose();
            image.Save(imageOutputPath);
            image.Dispose();
        }

        public static Stream DrawImage(string imageInputPath, string watermarkText, Font font, Color frontColor, WatermarkPosition watermarkPosition)
        {
            Image image = Image.FromFile(imageInputPath);
            Bitmap bitmap = new Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb);
            Graphics g = Graphics.FromImage(bitmap);
            g.Clear(Color.White);
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.High;
            g.DrawImage(image, 0, 0, image.Width, image.Height);
            Brush brush = new SolidBrush(frontColor);
            g.DrawString(watermarkText, font, brush, GetTextArea(g, bitmap, watermarkText, font, watermarkPosition));
            Stream resultStream = bitmap.ToStream(ImageFormat.Jpeg);
            brush.Dispose();
            g.Dispose();
            bitmap.Dispose();
            image.Dispose();
            return resultStream;
        }

        public static Stream DrawImage(string imageInputPath, string watermarkText, Font font, Color frontColor, Rectangle rectangle)
        {
            Image image = Image.FromFile(imageInputPath);
            Bitmap bitmap = new Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb);
            Graphics g = Graphics.FromImage(bitmap);
            g.Clear(Color.White);
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.High;
            g.DrawImage(image, 0, 0, image.Width, image.Height);
            Brush brush = new SolidBrush(frontColor);
            g.DrawString(watermarkText, font, brush, rectangle);
            Stream resultStream = bitmap.ToStream(ImageFormat.Jpeg);
            brush.Dispose();
            g.Dispose();
            bitmap.Dispose();
            image.Dispose();
            return resultStream;
        }

        private static RectangleF GetTextArea(Graphics g, Image image, string watermarkText, Font font, WatermarkPosition watermarkPosition)
        {
            SizeF size = g.MeasureString(watermarkText, font);
            float x, y, width = size.Width, height = size.Height;
            switch (watermarkPosition)
            {
                case WatermarkPosition.TopLeft:
                    x = 0;
                    y = 0;                    
                    break;
                case WatermarkPosition.TopCenter:
                    x = (image.Width-width)/2;
                    y = 0;
                    break;
                case WatermarkPosition.TopRight:
                    x = image.Width - width;
                    y = 0;
                    break;
                case WatermarkPosition.MiddleLeft:
                    x = 0;
                    y = (image.Height - height) / 2;
                    break;
                case WatermarkPosition.MiddleCenter:
                    x = (image.Width - width) / 2;
                    y = (image.Height - height) / 2;
                    break;
                case WatermarkPosition.MiddleRight:
                    x = image.Width - width;
                    y = (image.Height - height) / 2;
                    break;
                case WatermarkPosition.BottomLeft:
                    x = 0;
                    y = image.Height - height;
                    break;
                case WatermarkPosition.BottomCenter:
                    x = (image.Width - width) / 2;
                    y = image.Height - height;
                    break;
                case WatermarkPosition.BottomRight:
                default:
                    x = image.Width - width;
                    y = image.Height - height;
                    break;
            }
            return new RectangleF(x, y, width, height);
        }
    }
}
